package com.jiangli.controller.admin;

import com.jiangli.annotation.Log;
import com.jiangli.dto.*;
import com.jiangli.entity.Ingredient;
import com.jiangli.result.PageResult;
import com.jiangli.result.Result;
import com.jiangli.service.AdminDishService;
import com.jiangli.vo.DishVO;
import com.jiangli.vo.IngredientVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("admin/dish")
@Api(tags = "管理端菜品接口")
public class DishController {

    private final AdminDishService adminDishService;

    public DishController(AdminDishService adminDishService) {
        this.adminDishService = adminDishService;
    }

    @PostMapping
    @ApiOperation("新增菜品")
    @Log("新增菜品")
    public Result<String> addDish(@RequestBody DishDTO dishDTO) {
        try {
            adminDishService.addDish(dishDTO);
            return Result.success("添加成功");
        } catch (Exception e) {
            log.error("新增菜品失败: {}", e.getMessage(), e);
            throw e;
        }
    }

    @DeleteMapping
    @ApiOperation("删除菜品")
    @Log("删除菜品")
    public Result<String> deleteDish(@RequestParam(required = true) Long id) {
        try {
            adminDishService.deleteDish(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            log.error("删除菜品失败: {}", e.getMessage(), e);
            throw e; // 记录日志后抛出异常
        }
    }

    @PutMapping
    @ApiOperation("修改菜品")
    @Log("修改菜品")
    public Result<String> updateDish(@RequestBody DishUpdateDTO dishUpdateDTO) {
        try {
            adminDishService.updateDish(dishUpdateDTO);
            return Result.success("修改成功");
        } catch (Exception e) {
            log.error("修改菜品失败: {}", e.getMessage(), e);
            throw e; // 记录日志后抛出异常
        }
    }

    @GetMapping
    @ApiOperation("分页查询菜品")
    public Result<PageResult> getDishByPage(DishPageQueryDTO dishPageQueryDTO) {
        try {
            PageResult pageResult = adminDishService.getDishByPage(dishPageQueryDTO);
            return Result.success(pageResult);
        } catch (Exception e) {
            log.error("分页查询菜品失败: {}", e.getMessage(), e);
            throw e; // 记录日志后抛出异常
        }
    }

    @GetMapping("/detail")
    @ApiOperation("获取菜品详情")
    public Result<DishVO> getDishById(@RequestParam(required = true) Long id) {
        try {
            DishVO dish = adminDishService.getDishDetial(id);
            return Result.success(dish);
        } catch (Exception e) {
            log.error("获取菜品详情失败: {}", e.getMessage(), e);

            throw e; // 记录日志后抛出异常
        }
    }

    @PostMapping("/addIngredient")
    @ApiOperation("添加材料")
    @Log("添加材料")
    public Result<String> addIngredient(@RequestBody DishIngredientDTO dishIngredientDTO) {
        try {
            adminDishService.addIngredient(dishIngredientDTO);
            return Result.success("添加成功");
        } catch (Exception e) {
            log.error("添加材料失败: {}", e.getMessage(), e);
            throw e; // 记录日志后抛出异常
        }
    }

    @GetMapping("/getUserRating")
    @ApiOperation("获取用户评价")
    public Result<PageResult> getUserRating(
            @RequestParam(required = true) Long id,
            @RequestParam(required = false, defaultValue = "1") Long page,
            @RequestParam(required = false, defaultValue = "10") Long pageSize) {
        try {
            PageResult pageResult = adminDishService.getUserRating(id, page, pageSize);
            return Result.success(pageResult);
        } catch (Exception e) {
            log.error("获取用户评价失败: {}", e.getMessage(), e);
            throw e; // 记录日志后抛出异常
        }
    }

    @PostMapping("/flavor")
    @ApiOperation("添加口味或修改")
    @Log("添加口味")
    public Result<String> addFlavor(@RequestBody DishFlavorDTO dishFlavorDTO) {
        try {
            adminDishService.addFlavor(dishFlavorDTO);
            return Result.success("添加成功");
        } catch (Exception e) {
            log.error("添加口味失败: {}", e.getMessage(), e);
            throw e; // 记录日志后抛出异常
        }
    }

    @GetMapping("/Ingredients")
    @ApiOperation("获取材料列表")
    public Result<List<Ingredient>> getIngredients(@RequestParam String ingredientName) {
        try {
            List<Ingredient> lists = adminDishService.getIngredients(ingredientName);
            return Result.success(lists);
        } catch (Exception e) {
            log.error("获取材料列表失败: {}", e.getMessage(), e);
            throw e; // 记录日志后抛出异常
        }
    }
}
